# api获取人脸属性
# import requests
# import json
# subscription_key = "9415313ee78e4d79a0d6cf603d0177f0"
#
# assert subscription_key
# face_api_url = 'https://api-gzf123.cognitiveservices.azure.com/face/v1.0/detect'
#
# # 请求正文body
# image_url = 'https://www.somagnews.com/wp-content/uploads/2020/06/10-8-e1591815455106.jpg'
# headers = {'Ocp-Apim-Subscription-Key': subscription_key}
#
# # 请求参数parameters
# params = {
#     'returnFaceId': 'true',
#     'returnFaceLandmarks': 'false',
#     'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',
# }
#
# r = response = requests.post(face_api_url, params=params,
#                          headers=headers, json={"url": image_url})
# r.json()

# json转译结果
r = [{'faceId': 'e1e09dd0-5ac2-4fd8-b7e2-fcf82998b197',
  'faceRectangle': {'top': 157, 'left': 761, 'width': 87, 'height': 87},
  'faceAttributes': {'smile': 0.0,
   'headPose': {'pitch': -5.1, 'roll': -0.0, 'yaw': -6.9},
   'gender': 'female',
   'age': 22.0,
   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},
   'glasses': 'NoGlasses',
   'emotion': {'anger': 0.0,
    'contempt': 0.0,
    'disgust': 0.0,
    'fear': 0.0,
    'happiness': 0.0,
    'neutral': 0.997,
    'sadness': 0.003,
    'surprise': 0.0},
   'blur': {'blurLevel': 'low', 'value': 0.0},
   'exposure': {'exposureLevel': 'overExposure', 'value': 0.8},
   'noise': {'noiseLevel': 'medium', 'value': 0.55},
   'makeup': {'eyeMakeup': True, 'lipMakeup': True},
   'accessories': [],
   'occlusion': {'foreheadOccluded': False,
    'eyeOccluded': False,
    'mouthOccluded': False},
   'hair': {'bald': 0.03,
    'invisible': False,
    'hairColor': [{'color': 'blond', 'confidence': 0.99},
     {'color': 'red', 'confidence': 0.87},
     {'color': 'other', 'confidence': 0.69},
     {'color': 'gray', 'confidence': 0.34},
     {'color': 'brown', 'confidence': 0.15},
     {'color': 'black', 'confidence': 0.0},
     {'color': 'white', 'confidence': 0.0}]}}},
 {'faceId': '3e0e8bde-ea21-490f-995b-f9fc36d4f823',
  'faceRectangle': {'top': 164, 'left': 141, 'width': 84, 'height': 84},
  'faceAttributes': {'smile': 0.0,
   'headPose': {'pitch': -12.5, 'roll': -9.3, 'yaw': -18.5},
   'gender': 'female',
   'age': 22.0,
   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},
   'glasses': 'NoGlasses',
   'emotion': {'anger': 0.0,
    'contempt': 0.0,
    'disgust': 0.0,
    'fear': 0.0,
    'happiness': 0.0,
    'neutral': 1.0,
    'sadness': 0.0,
    'surprise': 0.0},
   'blur': {'blurLevel': 'low', 'value': 0.0},
   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.56},
   'noise': {'noiseLevel': 'medium', 'value': 0.41},
   'makeup': {'eyeMakeup': True, 'lipMakeup': True},
   'accessories': [],
   'occlusion': {'foreheadOccluded': False,
    'eyeOccluded': False,
    'mouthOccluded': False},
   'hair': {'bald': 0.06,
    'invisible': False,
    'hairColor': [{'color': 'brown', 'confidence': 1.0},
     {'color': 'red', 'confidence': 0.78},
     {'color': 'blond', 'confidence': 0.72},
     {'color': 'black', 'confidence': 0.05},
     {'color': 'gray', 'confidence': 0.05},
     {'color': 'other', 'confidence': 0.04},
     {'color': 'white', 'confidence': 0.0}]}}},
 {'faceId': 'ff2437ca-9e69-4345-b645-e4888f373df9',
  'faceRectangle': {'top': 173, 'left': 343, 'width': 82, 'height': 82},
  'faceAttributes': {'smile': 0.0,
   'headPose': {'pitch': -9.1, 'roll': 11.5, 'yaw': 16.2},
   'gender': 'female',
   'age': 22.0,
   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},
   'glasses': 'NoGlasses',
   'emotion': {'anger': 0.0,
    'contempt': 0.0,
    'disgust': 0.0,
    'fear': 0.0,
    'happiness': 0.0,
    'neutral': 0.999,
    'sadness': 0.001,
    'surprise': 0.0},
   'blur': {'blurLevel': 'low', 'value': 0.0},
   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.62},
   'noise': {'noiseLevel': 'low', 'value': 0.04},
   'makeup': {'eyeMakeup': True, 'lipMakeup': True},
   'accessories': [],
   'occlusion': {'foreheadOccluded': False,
    'eyeOccluded': False,
    'mouthOccluded': False},
   'hair': {'bald': 0.06,
    'invisible': False,
    'hairColor': [{'color': 'brown', 'confidence': 0.99},
     {'color': 'black', 'confidence': 0.81},
     {'color': 'gray', 'confidence': 0.22},
     {'color': 'red', 'confidence': 0.19},
     {'color': 'blond', 'confidence': 0.17},
     {'color': 'other', 'confidence': 0.08},
     {'color': 'white', 'confidence': 0.0}]}}},
 {'faceId': 'efdb6d32-f1eb-49a1-8a0a-aecf0651c453',
  'faceRectangle': {'top': 176, 'left': 571, 'width': 81, 'height': 81},
  'faceAttributes': {'smile': 0.0,
   'headPose': {'pitch': -10.4, 'roll': -7.7, 'yaw': -5.4},
   'gender': 'female',
   'age': 23.0,
   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},
   'glasses': 'NoGlasses',
   'emotion': {'anger': 0.0,
    'contempt': 0.0,
    'disgust': 0.0,
    'fear': 0.0,
    'happiness': 0.0,
    'neutral': 0.998,
    'sadness': 0.002,
    'surprise': 0.0},
   'blur': {'blurLevel': 'low', 'value': 0.0},
   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.65},
   'noise': {'noiseLevel': 'low', 'value': 0.0},
   'makeup': {'eyeMakeup': True, 'lipMakeup': True},
   'accessories': [],
   'occlusion': {'foreheadOccluded': False,
    'eyeOccluded': False,
    'mouthOccluded': False},
   'hair': {'bald': 0.23,
    'invisible': False,
    'hairColor': [{'color': 'brown', 'confidence': 0.99},
     {'color': 'black', 'confidence': 0.98},
     {'color': 'gray', 'confidence': 0.32},
     {'color': 'other', 'confidence': 0.13},
     {'color': 'red', 'confidence': 0.07},
     {'color': 'blond', 'confidence': 0.02},
     {'color': 'white', 'confidence': 0.0}]}}}]
# 建立字典
glasses = {}
smile = {}
hairColor = {}
age = {}
gender = {}

# 把所有人脸的值添加进字典
count = 0
while count < len(r):
    glasses["Person{}".format(count + 1)] = r[count]['faceAttributes']['glasses']
    smile["Person{}".format(count + 1)] = r[count]['faceAttributes']['smile']
    hairColor["Person{}".format(count + 1)] = r[count]['faceAttributes']['hair']['hairColor'][0]['color']
    age["Person{}".format(count + 1)] = r[count]['faceAttributes']['age']
    gender["Person{}".format(count + 1)] = r[count]['faceAttributes']['gender']
    count += 1

# 打印字典
print("眼镜：", glasses)
print("微笑指数：", smile)
print("头发颜色：", hairColor)
print("年龄：", age)
print("性别：", gender)

# 返回结果
# 眼镜： {'Person1': 'NoGlasses', 'Person2': 'NoGlasses', 'Person3': 'NoGlasses', 'Person4': 'NoGlasses'}
# 微笑指数： {'Person1': 0.0, 'Person2': 0.0, 'Person3': 0.0, 'Person4': 0.0}
# 头发颜色： {'Person1': 'blond', 'Person2': 'brown', 'Person3': 'brown', 'Person4': 'brown'}
# 年龄： {'Person1': 22.0, 'Person2': 22.0, 'Person3': 22.0, 'Person4': 23.0}
# 性别： {'Person1': 'female', 'Person2': 'female', 'Person3': 'female', 'Person4': 'female'}